﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using Google.GData.Contacts;
using PhoneManager.LanguageText;
namespace PhoneManager
{
    public partial class FrmBookSync : FrmDefault
    {
        public FrmBookSync()
        {
            InitializeComponent();
        }
        public BookSyncEnum TypeSync { get; set; }
        public bool SilentMode { get; set; }
        private void FrmBookSync_Shown(object sender, EventArgs e)
        {
            if (TypeSync == BookSyncEnum.SIM || TypeSync == BookSyncEnum.Phone)
            {
                this.Close();
            }
            if (SilentMode)
            {
                this.Opacity = 0;
            }
            int qtd = 0;
            switch (TypeSync)
            {
                case BookSyncEnum.Live:
                    {
                        if (!SilentMode)
                        {
                            this.Text = ResourcesText.LiveImport;
                            this.Cursor = Cursors.WaitCursor;
                            BntImport.Enabled = false;
                        }
                        foreach (Microsoft.LiveFX.Client.Contact item in SharedLib.LiveOpeEnv.Contacts.Entries)
                        {
                            if (item.Resource.PhoneNumbers.Count > 0)
                            {
                                for (int i = 0; i < item.Resource.PhoneNumbers.Count; i++)
                                {
                                    UCImportPhoneBook template = new UCImportPhoneBook();
                                    template.TypeFrom =BookSyncEnum.Live;
                                    template.OriginalID = item.Resource.ContactId.ToString();
                                    template.NumberContact = item.Resource.PhoneNumbers[i].Value;
                                    template.TypePhone = item.Resource.PhoneNumbers[i].Type.ToString();
                                    if (item.Resource.PhoneNumbers[i].Primary)
                                    {
                                        template.NameContact = item.Resource.PreferredUserName.Replace("_", " ").Trim();
                                    }
                                    else
                                    {
                                        template.NameContact = item.Resource.PreferredUserName.Replace("_", " ").Trim() + " (" + item.Resource.PhoneNumbers[i].Type.ToString() + ")";
                                    }
                                    bool Mark = false;
                                    LiveGooglePhoneBook pb = SharedLib.FindPhoneByFromLiveGoogleID(BookSyncEnum.Live, item.Resource.PhoneNumbers[i].Type.ToString(), item.Resource.ContactId.ToString());
                                    if (pb == null)
                                    {
                                        Mark = true;
                                        qtd++;
                                    }
                                    else
                                    {
                                        if (item.Resource.PhoneNumbers[i].Value != pb.OriginalNumber)
                                        {
                                            Mark = true;
                                            qtd++;
                                        }
                                    }
                                    template.Checked = Mark;
                                    flowLayoutPanel.Controls.Add(template);
                                }
                            }
                        }
                        if (qtd == 0)
                        {
                            if (!SilentMode)
                            {
                                SharedLib.MsgAlert(ResourcesText.LiveNoChange);
                                BntImport.Enabled = true;
                                this.Cursor = Cursors.Default;
                            }
                            this.Close();
                        }
                        else
                        {
                            if (!SilentMode)
                            {
                                SharedLib.MsgAlert(ResourcesText.WordHave + " " + qtd.ToString() + " " + ResourcesText.LiveChange );
                            }
                        }
                    }
                    break;
                case BookSyncEnum.Goolgle:
                    {
                        if (!SilentMode)
                        {
                            this.Text = ResourcesText.GoogleImport;
                            this.Cursor = Cursors.WaitCursor;
                            BntImport.Enabled = false;
                        }
                        ContactsQuery query = new ContactsQuery(ContactsQuery.CreateContactsUri("default"));
                        query.NumberToRetrieve = 500;
                        ContactsFeed feed = SharedLib.Googleservice.Query(query);
                        foreach (ContactEntry item in feed.Entries)
                        {
                            if (item.Phonenumbers.Count > 0)
                            {
                                for (int i = 0; i < item.Phonenumbers.Count; i++)
                                {
                                    UCImportPhoneBook template = new UCImportPhoneBook();
                                    template.TypeFrom = BookSyncEnum.Goolgle;
                                    string id = item.Id.Uri.ToString();
                                    int posid = id.LastIndexOf('/');
                                    id = id.Substring(posid + 1);
                                    template.OriginalID = id;
                                    template.NumberContact = item.Phonenumbers[i].Value;
                                    string typepb = item.Phonenumbers[i].Rel;
                                    int postype = typepb.LastIndexOf('#');
                                    typepb = typepb.Substring(postype + 1);
                                    template.TypePhone = typepb;
                                    if (item.Phonenumbers[i].Primary)
                                    {
                                        template.NameContact = item.Title.Text.Replace("_", " ").Trim();
                                    }
                                    else
                                    {
                                        template.NameContact = item.Title.Text.Replace("_", " ").Trim() + " (" + typepb + ")";
                                    }
                                    bool Mark = false;
                                    LiveGooglePhoneBook pb = SharedLib.FindPhoneByFromLiveGoogleID(BookSyncEnum.Goolgle, typepb, id);
                                    if (pb == null)
                                    {
                                        Mark = true;
                                        qtd++;
                                    }
                                    else
                                    {
                                        if (item.Phonenumbers[i].Value != pb.OriginalNumber)
                                        {
                                            Mark = true;
                                            qtd++;
                                        }
                                    }
                                    template.Checked = Mark;
                                    flowLayoutPanel.Controls.Add(template);
                                }
                            }
                        }
                        if (qtd == 0)
                        {
                            if (!SilentMode)
                            {
                                SharedLib.MsgAlert(ResourcesText.GoogleNoChange);
                                BntImport.Enabled = true;
                                this.Cursor = Cursors.Default;
                            }
                            this.Close();
                        }
                        else
                        {
                            if (!SilentMode)
                            {
                                SharedLib.MsgAlert(ResourcesText.WordHave +  " " + qtd.ToString() + " " + ResourcesText.GoogleChange);
                            }
                        }
                    }
                    break;
            }
            if (!SilentMode)
            {
                BntImport.Enabled = true;
                this.Cursor = Cursors.Default;
            }
            if (SilentMode && qtd > 0)
            {
                BntImport.PerformClick();
            }
            if (SilentMode && qtd == 0)
            {
                this.Close();
            }
        }

        private void BntCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void BntImport_Click(object sender, EventArgs e)
        {
            bool okCheck = false;
            foreach (UCImportPhoneBook item in flowLayoutPanel.Controls)
            {
                if (item.Checked)
                {
                    okCheck = true;
                    switch (item.TypeFrom)
                    {
                    case BookSyncEnum.Live:
                    case BookSyncEnum.Goolgle:
                        {
                            LiveGooglePhoneBook Selpb = new LiveGooglePhoneBook();
                            Selpb.OriginalID = item.OriginalID.Trim();
                            Selpb.OriginalName = item.NameContact.Trim();
                            Selpb.OriginalNumber = item.NumberContact.Trim();
                            Selpb.OriginalNumberType = item.TypePhone.Trim();
                            Selpb.OriginalFrom = item.TypeFrom;
                            Selpb.Name = "";
                            Selpb.Number = "";
                            Selpb.PhoneIndex = "";
                            Selpb.SIMIndex = "";
                            Selpb.IsSync = false;
                            LiveGooglePhoneBook pb = SharedLib.FindPhoneByFromLiveGoogleID(item.TypeFrom, item.TypePhone, item.OriginalID);
                            if (pb != null)
                            {
                                Selpb.Name = pb.Name.Trim();
                                Selpb.Number = pb.Number.Trim();
                                Selpb.PhoneIndex = pb.PhoneIndex.Trim();
                                Selpb.SIMIndex = pb.SIMIndex.Trim();
                                if (pb.OriginalNumber == Selpb.OriginalNumber && pb.Number != "" && pb.Name != "")
                                {
                                    Selpb.OriginalNumber = pb.OriginalNumber.Trim();
                                    Selpb.IsSync = true;
                                }

                                //validade indexs
                                if (Selpb.PhoneIndex != "")
                                {
                                    PhoneBook pbm = SharedLib.FindPhoneByID(Selpb.PhoneIndex, false);
                                    if (pbm == null)
                                    {
                                        Selpb.PhoneIndex = "";
                                        Selpb.IsSync = false;
                                    }
                                    else
                                    {
                                        if (Selpb.Name != pbm.Name || Selpb.Number != pbm.Number)
                                        {
                                            Selpb.Name = pbm.Number;
                                            Selpb.Number = pbm.Number;
                                            Selpb.IsSync = false;
                                        }
                                    }
                                }
                                if (Selpb.SIMIndex != "")
                                {
                                    PhoneBook pbm = SharedLib.FindPhoneByID(Selpb.SIMIndex, true);
                                    if (pbm == null)
                                    {
                                        Selpb.SIMIndex = "";
                                        Selpb.IsSync = false;
                                    }
                                    else
                                    {
                                        if (Selpb.Name != pbm.Name || Selpb.Number != pbm.Number)
                                        {
                                            Selpb.Name = pbm.Number;
                                            Selpb.Number = pbm.Number;
                                            Selpb.IsSync = false;
                                        }
                                    }
                                }

                                if (item.TypeFrom == BookSyncEnum.Live)
                                {
                                    SharedLib.LiveContacts.Remove(pb);
                                }
                                else if (item.TypeFrom == BookSyncEnum.Goolgle)
                                {
                                    SharedLib.GoogleContacts.Remove(pb);
                                }
                            }
                            if (item.TypeFrom == BookSyncEnum.Live)
                            {
                                SharedLib.LiveContacts.Add(Selpb);
                            }
                            else if (item.TypeFrom == BookSyncEnum.Goolgle)
                            {
                                SharedLib.GoogleContacts.Add(Selpb);
                            }
                        }
                        break;
                    }
                }
            }

            if (!SilentMode && okCheck)
            {
                SharedLib.ShowPhoneBook = true;
            }
            else
            {
                SharedLib.ShowPhoneBook = false;
            }
            if (okCheck && TypeSync != BookSyncEnum.SIM && TypeSync != BookSyncEnum.Phone)
            {
                SharedLib.ShowPhoneSync = true;
            }
            else
            {
                SharedLib.ShowPhoneSync = false;
            }
            this.Close();
        }
    }
}
